package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.SenSitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SenSitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements SenSitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 查询敏感词列表
     * @return
     */
    @Override
    public ResponseResult getList(WmSensitiveDto dto) {

        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());

        wmSensitiveMapper.selectPage(page, Wrappers.<WmSensitive>lambdaQuery()
                .eq(!dto.getName().equals(""),WmSensitive::getSensitives,dto.getName())
        );


        PageResponseResult pageResponseResult =
                new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());

        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;

    }

    /**
     * 新增敏感词
     * @param map
     * @return
     */
    @Override
    public ResponseResult saveSensitive(Map<String,String> map) {
        if (map.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(map.get("sensitives"));
        wmSensitive.setCreatedTime(new Date());

        wmSensitiveMapper.insert(wmSensitive);

        return ResponseResult.okResult("添加敏感词成功");
    }

    /**
     * 修改敏感词信息
     * @param map
     * @return
     */
    @Override
    public ResponseResult updateSensitive(Map<String, String> map) {
        if (map.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmSensitive wmSensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, map.get("id")));
        wmSensitive.setSensitives(map.get("sensitives"));
        wmSensitive.setCreatedTime(new Date());

        wmSensitiveMapper.updateById(wmSensitive);

        return ResponseResult.okResult("修改敏感词信息成功");
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delectSensitive(int id) {
        if (id == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        wmSensitiveMapper.deleteById(id);

        return ResponseResult.okResult("删除成功");
    }

    @Override
    public List<WmSensitive> selectList() {
        List<WmSensitive> list = this.list();
        return list;
    }
}
